package com.lidy.tankbattle.event.tankgame1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import com.lidy.tankbattle.tankgame1.constant.TankPriorities;

/**
 * @author lidy
 * @version V1.0
 * @classname TankPanel1
 * @description 游戏操作界面
 * @date 2022/5/28 15:29
 */
public class TankPanel1 extends JPanel implements KeyListener {
    /**
     * 己方坦克
    **/
    Hero hero;
    /**
     * 敌方坦克
    **/
    Vector<EnemyTank> enemyTanks = new Vector<>();
    private final int PANEL_WIDTH;

    private final int PANEL_HEIGHT;

    public TankPanel1(int panelWidth, int panelHeight) {
        PANEL_WIDTH = panelWidth;
        PANEL_HEIGHT = panelHeight;
    }

    public void addTanks(int initX, int initY, int initDirection) {
        hero = new Hero(initX, initY, initDirection);
        for (int i = 0; i < TankPriorities.ENEMY_TANK_NUMBER; i++) {
            enemyTanks.add(new EnemyTank(200 * i + 100, 0, TankPriorities.DIRECTION_DOWN));
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);
        // 画坦克
        drawTank(hero.getX(), hero.getY(), g, hero.getDirection(), hero.getType());
        for (EnemyTank enemyTank : enemyTanks) {
            drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), enemyTank.getType());
        }
    }

    /**
     * @author: lidy
     * @description: 画坦克
     * @date: 2022/5/28 15:57
     */
    public void drawTank(int x, int y, Graphics g, int direction, int type) {
        switch (type) {
            // 我们的坦克
            case 0:
                g.setColor(Color.cyan);
                break;
            // 敌人的坦克
            case 1:
                g.setColor(Color.yellow);
                break;
            default:
                break;
        }

        switch (direction) {
            // 向上
            case TankPriorities.DIRECTION_UP:
                // 左边轮子
                g.fill3DRect(x, y, 10, 60, false);
                // 右边轮子
                g.fill3DRect(x + 30, y, 10, 60, false);
                // 车厢
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                // 炮筒
                g.drawLine(x + 20, y, x + 20, y + 20);
                // 炮塔
                g.fillOval(x + 10, y + 20, 20, 20);
                break;
            // 向下
            case TankPriorities.DIRECTION_DOWN:
                // 左边轮子
                g.fill3DRect(x, y, 10, 60, false);
                // 右边轮子
                g.fill3DRect(x + 30, y, 10, 60, false);
                // 车厢
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                // 炮筒
                g.drawLine(x + 20, y + 40, x + 20, y + 60);
                // 炮塔
                g.fillOval(x + 10, y + 20, 20, 20);
                break;
            // 向左
            case TankPriorities.DIRECTION_LEFT:
                // 上边轮子
                g.fill3DRect(x, y, 60, 10, false);
                // 下边轮子
                g.fill3DRect(x, y + 30, 60, 10, false);
                // 车厢
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                // 炮筒
                g.drawLine(x, y + 20, x + 20, y + 20);
                // 炮塔
                g.fillOval(x + 20, y + 10, 20, 20);
                break;
            // 向右
            case TankPriorities.DIRECTION_RIGHT:
                // 上边轮子
                g.fill3DRect(x, y, 60, 10, false);
                // 下边轮子
                g.fill3DRect(x, y + 30, 60, 10, false);
                // 车厢
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                // 炮筒
                g.drawLine(x + 40, y + 20, x + 60, y + 20);
                // 炮塔
                g.fillOval(x + 20, y + 10, 20, 20);
                break;
            default:
                break;
        }
    }

    /**
     * 有字符输出时，该方法会触发
     **/
    @Override
    public void keyTyped(KeyEvent e) {

    }

    /**
     * 当某个键按下时，该方法会触发
     **/
    @Override
    public void keyPressed(KeyEvent e) {
        // 根据按下的不同键，来处理小球的移动（上下左右移动）
        // 向下的箭头
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            heroTankMove(TankPriorities.DIRECTION_DOWN);
        } else if (e.getKeyCode() == KeyEvent.VK_UP) {
            heroTankMove(TankPriorities.DIRECTION_UP);
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            heroTankMove(TankPriorities.DIRECTION_LEFT);
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            heroTankMove(TankPriorities.DIRECTION_RIGHT);
        }
    }

    /**
     * 己方坦克移动
     **/
    private void heroTankMove(int destDirection) {
        hero.setDirection(destDirection);
        // 目标方向和当前方向相同就是移动
        switch (destDirection) {
            case TankPriorities.DIRECTION_UP:
                if (hero.getY() > 0) {
                    hero.moveUp();
                }
                break;
            case TankPriorities.DIRECTION_DOWN:
                if (hero.getY() < PANEL_HEIGHT) {
                    hero.moveDown();
                }
                break;
            case TankPriorities.DIRECTION_LEFT:
                if (hero.getX() > 0) {
                    hero.moveLeft();
                }
                break;
            case TankPriorities.DIRECTION_RIGHT:
                if (hero.getX() < PANEL_WIDTH) {
                    hero.moveRight();
                }
                break;
            default:
                break;
        }
        this.repaint();
    }

    /**
     * 己方坦克移动
     **/
    // private void heroTankMove(int destDirection) {
    //     int currentDirection = hero.getDirection();
    //     // 目标方向和当前方向相同就是移动
    //     if (currentDirection == destDirection) {
    //         switch (destDirection) {
    //             case TankConstants.DIRECTION_UP:
    //                 if (hero.getY() > 0) {
    //                     hero.moveUp();
    //                     this.repaint();
    //                 }
    //                 break;
    //             case TankConstants.DIRECTION_DOWN:
    //                 if (hero.getY() < PANEL_HEIGHT) {
    //                     hero.moveDown();
    //                     this.repaint();
    //                 }
    //                 break;
    //             case TankConstants.DIRECTION_LEFT:
    //                 if (hero.getX() > 0) {
    //                     hero.moveLeft();
    //                     this.repaint();
    //                 }
    //                 break;
    //             case TankConstants.DIRECTION_RIGHT:
    //                 if (hero.getX() < PANEL_WIDTH) {
    //                     hero.moveRight();
    //                     this.repaint();
    //                 }
    //                 break;
    //             default:
    //                 break;
    //         }
    //     } else { // 目标方向和当前方向不同是转向
    //         if (currentDirection == TankConstants.DIRECTION_UP || currentDirection == TankConstants.DIRECTION_DOWN) {
    //             if (destDirection == TankConstants.DIRECTION_LEFT || destDirection == TankConstants.DIRECTION_RIGHT) {
    //                 hero.setX(hero.getX() - 10);
    //                 hero.setY(hero.getY() + 10);
    //             }
    //         } else if (currentDirection == TankConstants.DIRECTION_LEFT || currentDirection == TankConstants.DIRECTION_RIGHT) {
    //             if (destDirection == TankConstants.DIRECTION_UP || destDirection == TankConstants.DIRECTION_DOWN) {
    //                 hero.setX(hero.getX() + 10);
    //                 hero.setY(hero.getY() - 10);
    //             }
    //         }
    //         hero.setDirection(destDirection);
    //         this.repaint();
    //     }
    // }

    /**
     * 当某个键放开时，该方法会触发
     **/
    @Override
    public void keyReleased(KeyEvent e) {

    }
}
